/*
 * @Author: 李环玉 834654733@qq.com
 * @Date: 2023-11-25 19:03:33
 * @LastEditors: 李环玉 834654733@qq.com
 * @LastEditTime: 2023-12-23 17:40:01
 * @FilePath: \koa-server\src\controller\user.controller.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const jwt = require('jsonwebtoken')
const { createUser,getUserInfo,resetPass,editInfo } = require('../service/user.service')
const { success,error } = require('../utils/response')
const { JWT_SECRET } = require('../config/config.default')
class userControllrt {
  async register(ctx,next) {
    const params = ctx.request.body
    try{
      const res = await createUser(params)
      if(res){
        success(ctx,res)
      } else {
        error(ctx)
      }
    } catch(err)  {
      error(ctx,null,'用户注册错误',502)
    }
  }
  async login (ctx,next){
    const { userName,passWord } = ctx.request.body
    try{
      const {passWord,...res} = await getUserInfo({userName})
      // const token = jwt.sign(res,JWT_SECRET,{expiresIn:'1d'})
      success(ctx,{...res},'登录成功')
    } catch(err) {
      error(ctx,null,err,502)
    }
  }
  async resetPassword(ctx,next){
    const params = ctx.request.body
    try{
      const res = await resetPass(params)
      success(ctx,res)
    }catch(err){
      error(ctx,null,err)
    }
    
  }
  async resetInfo(ctx,next){
    const params = ctx.request.body
    try{
      const res = await editInfo(params)
      success(ctx,res)
    }catch(err){
      error(ctx,null,err)
    }
  }
}

module.exports = new userControllrt()